<?php

namespace Lettral\LetterBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\Form\Form;
use Symfony\Component\HttpFoundation\Request;
use FOS\UserBundle\Model\UserManagerInterface;
use Lettral\LetterBundle\Form\LetterForm;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

class FormController extends Controller
{
    /**
     * @Route("/")
     * @Template()
     */
    public function listFormAction($type)
    {       
        $em = $this->container->get('doctrine')->getEntityManager();
        
        if ($type == "app") {
            $query = $em->createQuery('SELECT form.text FROM LettralLetterBundle:FormApp form');
        } else if ($type == "pol") {
            $query = $em->createQuery('SELECT form.text FROM LettralLetterBundle:FormPol form');
        } else {
            throw new AccessDeniedException();
        }
        
        $forms = $query->getResult(); 
        
        $response = new \Symfony\Component\HttpFoundation\Response();
        
        $response->headers->set('Content-Type', 'application/json; charset=UTF-8');
        $response->setContent(json_encode($forms));

        return $response;
    }
}
